package org.example.threadlocaltest.demos.configure;

import lombok.Data;
import org.example.threadlocaltest.demos.enums.UnifiedReturnEnum;

import java.time.LocalDateTime;

@Data
// 统一返回类
public class UnifiedReturn<T> {

    private int code;// 状态码  200正常  各种异常 需要在 枚举里面定义
    private String msg;// 返回信息
    private T data;// 返回数据
    private String now = LocalDateTime.now().toString().replace("T"," ");// 时间戳
    private Long usedTime;// 耗时

    public UnifiedReturn(T data) {
        this.data = data;
    }

    //success
    public static <T> UnifiedReturn<T> success(T data) {
        UnifiedReturn<T> aReturn = new UnifiedReturn<>(data);
        aReturn.setCode(UnifiedReturnEnum.SUCCESS.getCode());
        aReturn.setMsg(UnifiedReturnEnum.SUCCESS.getMsg());
        return aReturn;
    }

    //error
    public static <T> UnifiedReturn<T> error(int code, String msg) {
        UnifiedReturn<T> aReturn = new UnifiedReturn<>(null);
        aReturn.setCode(code);
        aReturn.setMsg(msg);
        return aReturn;
    }

}
